package com.yinchuang.aiuidemo;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 */
public class SdkRecordActivity extends AppCompatActivity implements AIUImanager.AIUIResultEvent {
    public static String TAG = "MainActivity";
    @BindView(R.id.tv)
    AppCompatTextView mTv;
    @BindView(R.id.btn_start)
    AppCompatButton mBtnStart;
    @BindView(R.id.btn_stop)
    AppCompatButton mBtnStop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdk);
        ButterKnife.bind(this);
        AIUImanager.setAIUIResultEvent(this);
        aiuImanager = AIUImanager.getInstance(this);
        aiuImanager.setText(mTv);
    }

    private AIUImanager aiuImanager;

    @OnClick({R.id.btn_start, R.id.btn_stop})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                mTv.setText("");
                if (aiuImanager != null && !isRecord) {
                    isRecord = true;
                    aiuImanager.startRecord();
                    Log.d(TAG, "recording start!!!");
                }
                break;
            case R.id.btn_stop:
                if (aiuImanager != null && isRecord) {
                    aiuImanager.stopRecord();
                    Log.d(TAG, "recording stop!!!");
                    isRecord = false;
                }
                break;
        }
    }

    private boolean isRecord;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_F5:
            case KeyEvent.KEYCODE_SEARCH:
                if (aiuImanager != null && !isRecord) {
                    isRecord = true;
                    aiuImanager.startRecord();
                    Log.d(TAG, "recording start!!!");
                }
                break;
            default:
                Log.d(TAG, "key release");
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_F5:
            case KeyEvent.KEYCODE_SEARCH:
                if (aiuImanager != null && isRecord) {
                    aiuImanager.stopRecord();
                    Log.d(TAG, "recording stop!!!");
                    isRecord = false;
                }
            default:
                Log.d(TAG, "key release");
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void onSuccess(final String result) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mTv.setText(result);
            }
        });
    }

}
